package jp.sourceforge.akjrcp.imageedit;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

/*
 * 900 image files uses 70MB memory.
 * maybe this is smart.
 * 
 */
public class BufferImageCasher {
private Map map=new HashMap();
public void clear(){
	map.clear();
}
public void putImage(Image image,String name){
	ByteArrayOutputStream bout=new ByteArrayOutputStream();
	ImageLoader loader=new ImageLoader();
	loader.data=new ImageData[]{image.getImageData()};
	loader.save(bout, SWT.IMAGE_JPEG);//don't use PNG
	
	//System.out.println(bout.toByteArray().length);
	map.put(name,bout.toByteArray());
}

public Image getImage(String name){
	Image image=null;
	byte[] bytes=(byte[]) map.get(name);
	if(bytes!=null){
		image=new Image(null,new ByteArrayInputStream(bytes));
	}
	return image;
}

}
